/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package first;

import com.sun.javafx.svgpath.util.io.Messages;
import java.awt.Dialog;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javax.swing.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class First extends Application {
    // private List<Integer> Point =new 

    int i = 0, j = 1;
    private Scene scene;
    private ChoiceBox classEquip = new ChoiceBox();
    private Label wave = new Label(" Wave length");
    private TextField waveLength = new TextField();//nhap buoc song
    Image imageDataBtn = new Image(getClass().getResourceAsStream("/Icon/data.png"));
    private Button dataBtn = new Button("Save", new ImageView(imageDataBtn));
    private Menu menu1 = new Menu("File");
    private Menu menu2 = new Menu("Edit");
    private Menu menu3 = new Menu("Help");
    private MenuBar menuBar = new MenuBar();
    Image imageGetLength = new Image(getClass().getResourceAsStream("/Icon/tick_16.png"));
    private Button getLength = new Button("Wave Length", new ImageView(imageGetLength));

    @Override
    public void start(Stage stage) {
        final Browser web = new Browser();
        //Pane nay chua 3 Pane left,Right,Center
        BorderPane mainLayout = new BorderPane();
        //Hien thi map
        VBox viewMap = new VBox();
        viewMap.getStyleClass().add("map");

        //Chua cac dieu khien WebView
        VBox controlPane1 = new VBox();
        // 
        VBox controlPane2 = new VBox();
        //Goi ham khoi tao cho panel 1
        paneControl1(web);
        //Pane Wave Length
        GridPane waveL = new GridPane();
        waveL.setHgap(10);
        waveL.add(wave, 0, 0);
        waveL.add(waveLength, 1, 0);
        //ProgressBar
        final Task<Void> task = new Task<Void>() {

            @Override
            public Void call() throws Exception {
                for (int k = 0; k <= 500; k++) {
                    updateProgress(k, 500);
                    Thread.sleep(200);
                }
                return null;
            }
        };
        ProgressBar progress = new ProgressBar();
        ProgressIndicator proIn = new ProgressIndicator();
        progress.setPrefSize(700, 10);
        proIn.setPrefSize(40, 40);
        progress.progressProperty().bind(task.progressProperty());
        proIn.progressProperty().bind(task.progressProperty());
        //Button luu co so du lieu
        dataBtn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                new Thread(task).start();
                web.webEngine.executeScript("document.getPath(0)");

            }
        });
        //Chua ClassEquip va progreIndicator
        GridPane cP = new GridPane();
        cP.getColumnConstraints().add(new ColumnConstraints(150));
        cP.getColumnConstraints().add(new ColumnConstraints(50));
        cP.add(dataBtn, 0, 0);
        cP.add(proIn, 1, 0);
        cP.setPrefWidth(210);
        menuBar.getMenus().addAll(menu1, menu2, menu3);
        //De chen vao khoang trong
        VBox space = new VBox();
        space.setPadding(new Insets(20, 10, 10, 10));
        space.setSpacing(20);
        space.getChildren().addAll(classEquip, cP, waveL, getLength);
        controlPane1.getChildren().addAll(menuBar, space);
        controlPane1.setSpacing(20);
        controlPane1.setPrefHeight(600);
        controlPane1.setPrefWidth(250);
        controlPane1.setStyle("-fx-background-color:white");
        controlPane2.setPrefHeight(600);
        controlPane2.setPrefWidth(250);
        viewMap.getChildren().addAll(web, progress);
        mainLayout.setRight(controlPane2);
        mainLayout.setCenter(viewMap);
        mainLayout.setLeft(controlPane1);
        stage.setTitle("FTTH NETWORK");
        scene = new Scene(mainLayout, 1200, 614, Color.web("#336699"));
        stage.setScene(scene);
        Image icon = new Image(getClass().getResourceAsStream("/Icon/globe.png"));
        stage.getIcons().add(icon);
        scene.getStylesheets().add(getClass().getResource("/Style/StyleSheet.css").toExternalForm());
        stage.show();
    }
//tao ra control pane dau tien

    public void paneControl1(final Browser web) {
        web.setPrefSize(800, 700);
//set cac tinh chat cua classEquip
        classEquip.setItems(FXCollections.observableArrayList("Choose Class", "Core Class", "Boundary Class", "Access Class", "OLT"));
        classEquip.getSelectionModel().selectFirst();
        classEquip.setTooltip(new Tooltip("Choose class"));
        classEquip.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if (newValue == 1) {
                    web.webEngine.executeScript("document.clickMarker1()");
                } else if (newValue == 2) {
                    web.webEngine.executeScript("document.clickMarker2()");
                } else if (newValue == 3) {
                    web.webEngine.executeScript("document.clickMarker3()");
                } else if (newValue == 4) {
                    web.webEngine.executeScript("document.clickMarker4()").toString();
                } else {
                    web.webEngine.executeScript("document.clearListener()");//Click ban do cung khong bao gi
                    String a = web.webEngine.executeScript("document.getClass1()").toString();
                    System.out.println(a);
                }

            }
        });
        classEquip.getStyleClass().add("class-Equip");

        //button gan buoc song
        getLength.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                String a = web.webEngine.executeScript("document.getDis()").toString();
                System.out.println(a);
            }
        });
        //validation trong text field
        waveLength.setOnKeyPressed(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent e) {
                if (e.getText().equals("-")) {
                    JOptionPane.showMessageDialog(null, e.getText());
                }
            }
        });
        MenuItem refresh = new MenuItem("Refresh",
                new ImageView(new Image(getClass().getResourceAsStream("/Icon/plus_16.png"))));
        refresh.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                web.webEngine.reload();
            }
        });
        refresh.setAccelerator(KeyCombination.keyCombination("F5"));
        refresh.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                web.webEngine.reload();
            }
        });

        MenuItem exit = new MenuItem("Exit", new ImageView(new Image(getClass().getResourceAsStream("/Icon/plus_16.png"))));
        exit.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                System.exit(0);
            }
        });
        exit.setAccelerator(KeyCombination.keyCombination("Ctrl+E"));
        exit.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                System.exit(0);
            }
        });

        menu2.getItems().addAll(refresh, exit);
//Set Action cho Menu refresh
        getLength.setPrefWidth(120);
        dataBtn.getStyleClass().add("data-Button");
        classEquip.setMaxWidth(120);
        dataBtn.setMaxWidth(120);
        classEquip.setMaxHeight(20);
        dataBtn.setMaxHeight(20);
        waveLength.setMaxWidth(60);
        wave.getStyleClass().add("wave");
    }
//tao ra panecontrol 2

    public void paneControl2() {
    }

    public static void main(String[] args) {
        launch(args);
    }
}
